package com.erp.module.duizhangPhoto

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.DuizhangBean
import com.erp.bean.PhotoBean
import com.erp.contacts.Contacts
import com.erp.contacts.Contacts.BOUNDARY
import com.erp.util.Httputil
import com.erp.util.Httputil.*
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.File

class DuizhangPhotoModel {

    /**获取对账拍照列表 */
    fun getDuizhangList(context: Context, page: Int, pagesize: Int,onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.DUIZHANG_PHOTO)
        getparams(context, params)
        setProsess(params)
        params.addBodyParameter("page", page.toString())
        params.addBodyParameter("pagesize", pagesize.toString())

        x.http().get(params, Call(context, onUserDataInterface))
    }
    /**获取对账拍照照片*/
    fun getDuizhangPhoto(context: Context, id: String,onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.DUIZHANG_PHOTO+"/"+id)
        getparams(context, params)
        setProsess(params)
        x.http().get(params, Call(context, onUserDataInterface))
    }

    /**上传删除对账拍照照片 */
    fun postDuizhangPhoto(context: Context, bean: DuizhangBean, deletephoto: List<PhotoBean>, onUserDataInterface: OnUserDataInterface) {
        var params = RequestParams(Contacts.DUIZHANG_PHOTO)
        getparams(context, params)
        addtoken(params)
        Log.d("postDuizhangPhoto",bean.toString())
        params!!.addHeader("ACCEPT", "application/json")
        params.addBodyParameter("year", bean.year, BOUNDARY)
        params.addBodyParameter("month", bean.month, BOUNDARY)
        for (i in bean.listphoto.indices) {
            val photo = bean.listphoto[i]
            if (photo.fid == "") {
                params.addBodyParameter("files[new][$i]", File(photo.uri), BOUNDARY)
            }
        }
        for (i in deletephoto.indices) {
            if (deletephoto[i].fid != "") {
                params.addBodyParameter("files[delete][" + deletephoto[i].fid + "]", deletephoto[i].fid, BOUNDARY)
            }
        }
        x.http().post<String>(params, Call(context, onUserDataInterface))
    }
}